#include <iostream>
#include <algorithm>
#include <xscript/xfilereader.h>
#include <xscript/xlexer.h>
#include <xscript/xcall.h>
#include <xscript/xprint.h>
#include <xscript/xscriptexecutor.h>

int main() {
    std::string file = "./../script/set.x";

    XFileReader reader;
    reader.open(file);

    XLexParser parser;

    std::shared_ptr<XError> error;

    int32_t line = 1;
    while (!reader.eof()) {
        auto text = reader.readLine();
        if (!parser.parse(text, line++)) {
            break;
        }
    }

    if (error)
        std::wcout << "parse error:" << XScriptExecutor::getInstance()->getErrorInfo() << std::endl;
    else {
        XScriptExecutor::getInstance()->setTokens(parser.getTokens());
        if (XScriptExecutor::getInstance()->execute()) {
            std::wcout << "execute success\n";
            //XScriptExecutor::getInstance()->getVariableTable()->printTable();
        } else {
            print(std::wcout, XScriptExecutor::getInstance()->getErrorInfo());
        }
    }
    return 1;
}
